package com.example.ivos.geofence.service.impl;

import com.example.ivos.base.exception.ServiceException;
import com.example.ivos.base.response.StatusCode;
import com.example.ivos.geofence.mapper.GeofenceMapper;
import com.example.ivos.geofence.pojo.dto.GeofenceParam;
import com.example.ivos.geofence.pojo.dto.GeofenceQuery;
import com.example.ivos.geofence.pojo.entity.Geofence;
import com.example.ivos.geofence.pojo.vo.GeofenceVO;
import com.example.ivos.geofence.service.GeofenceService;
import com.example.ivos.vehicle.pojo.dto.VehicleQuery;
import com.example.ivos.vehicle.pojo.vo.VehicleVO;
import com.example.ivos.vehicle.service.VehicleService;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    private GeofenceMapper geofenceMapper;

    @Autowired
    private VehicleService vehicleService;

    @Override
    public List<GeofenceVO> select(GeofenceQuery geofenceQuery) {
        log.debug("围栏页表业务geofenceQuery:{}",geofenceQuery);
        List<GeofenceVO> list=geofenceMapper.select(geofenceQuery);
        for(int i=0;i<list.size();i++){
            GeofenceVO geofenceVo=list.get(i);
            updateGeofenceVO(geofenceVo);
        }
        return list;
    }

    private void updateGeofenceVO(GeofenceVO geofenceVo){
        int totalNum=0;
        int availableNum=0;
        List<VehicleVO> vehicleList=new ArrayList<>();
        VehicleQuery vehicleQuery=new VehicleQuery();
        vehicleQuery.setGeofenceId(geofenceVo.getId());
        List<VehicleVO> select = vehicleService.select(vehicleQuery);
        if(select!=null && select.size()>0){
            for(int i=0;i<select.size();i++){
                VehicleVO vehicleVO=select.get(i);
                vehicleList.add(vehicleVO);
                if(vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
                totalNum++;
            }
        }
        geofenceVo.setTotalNum(totalNum);
        geofenceVo.setAvailableNum(availableNum);
        geofenceVo.setVehicleList(vehicleList);
    }

    @Override
    public void save(GeofenceParam geofenceParam) {
        log.debug("管理围栏业务geofenceParam:{}",geofenceParam);
        Geofence geofence=new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
        if(geofence.getId()==null){
            geofence.setCreateTime(new Date());
            geofence.setStatus("1");
            geofenceMapper.add(geofence);
        }else{
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        }
    }

    @Override
    public void del(Long id) {
        log.debug("删除围栏业务id:{}",id);
//        geofenceMapper.del(id); //通过前端判定是否存有绑定车辆

        //通过后端判断是否存有绑定车辆
        //业务：需要判断该围栏上是否有绑定车辆，如果有绑定车辆，就不能删除
        //1.封装查询车辆的DTO层对象
        VehicleQuery vehicleQuery=new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        //2.调用车辆模块查询方法，查询指定围栏下的所有车辆数据
        List<VehicleVO> vehicleIdList=vehicleService.select(vehicleQuery);
        //3.判断车辆列表是否为空，为空表示没有绑定车辆，可以删除围栏，否则不能删除
        if(vehicleIdList.size()>0&&vehicleIdList!=null){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else{
            //删除围栏
            geofenceMapper.del(id);
        }
    }
}
